package com.mdlib.helper.ui.fragments.rush.vicinity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.AppModel;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.model.entity.OrderEntity;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.ui.adapter.RushVicinityAdapter;
import com.mdlib.helper.utils.ToastUtil;
import com.sevenheaven.segmentcontrol.SegmentControl;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：HQY on 16/11/26 14:11
 * 邮箱：hqy_xz@126.com
 */

public class RushVicinityFragment extends BaseTitleFragment {

    @Bind(R.id.rv_rush_vicinity_list)
    RecyclerView mRvRushVicinityList;
    @Bind(R.id.sc_rush_vicinity)
    SegmentControl mScRushVicinity;

    private String type = "2";


    private List<OrderEntity> orderEntityList = new ArrayList<>();

    private RushVicinityAdapter mRushVicinityAdapter;


    public static RushVicinityFragment newInstance() {
        return new RushVicinityFragment();
    }


    public View.OnClickListener orderSetting = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addFragment(OrderSetFragment.newInstance());
        }
    };

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_rush_vicinity;
    }

    @Override
    public void onStart() {
        super.onStart();
        getVicinityOrder();
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.vicinity_tv_order_title, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getActivity().finish();
                    }
                })
                .setImgRightBg(R.drawable.iv_rush_order_setting)
                .setImgRightListener(orderSetting);

        init();
    }

    public void init() {
        getVicinityOrders();
        mScRushVicinity.setOnSegmentControlClickListener(new SegmentControl.OnSegmentControlClickListener() {
            @Override
            public void onSegmentControlClick(int index) {
                if (index == 0) {
                    type = "2";
                    getVicinityOrder();
                } else if (index == 1) {
                    type = "3";
                    getVicinityOrder();
                } else if (index == 2) {
                    type = "1";
                    getVicinityOrder();
                }
            }
        });

    }

    public void getVicinityOrders() {
        mRushVicinityAdapter = new RushVicinityAdapter(orderEntityList);
        mRvRushVicinityList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvRushVicinityList.setAdapter(mRushVicinityAdapter);
        mRushVicinityAdapter.notifyDataSetChanged();
        mRvRushVicinityList.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_vicinity_order_submit:
//                        addFragment(RushOrderDetailsFragment.newInstanct());
                        rushOrder(orderEntityList.get(position).getId() + "");
                        break;

                }
            }
        });


    }

    /**
     * 抢单
     *
     * @param busId
     */
    private void rushOrder(final String busId) {
        addSubscribe(DataManager.getInstance().getUserApi()
                .rusnOrder(busId)
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        UIHelper.showRushOrderDetails(getActivity(), busId);

                    }

                    @Override
                    protected void onFail(String msg) {

                        ToastUtil.showToast(msg);
                    }
                }));
    }

    /**
     * 获取订单
     */
    private void getVicinityOrder() {
        orderEntityList.clear();
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .getRunVicinityOrder(AppModel.getInstance().getAppLocationEntity().getLats(),
                        AppModel.getInstance().getAppLocationEntity().getLongs(), type)
                .compose(RxHelper.<ArrayList<OrderEntity>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<OrderEntity>>() {
                    @Override
                    protected void onSucc(ArrayList<OrderEntity> orderEntities) {
                        stopLoadingDialog();
                        orderEntityList = orderEntities;
                        mRushVicinityAdapter.setNewData(orderEntityList);
                        mRushVicinityAdapter.notifyDataSetChanged();
                    }

                    @Override
                    protected void onFail(String msg) {
                        stopLoadingDialog();
                        ToastUtil.showToast(msg);
                    }
                }));
    }
}
